package com.wyj.server;

import com.wyj.connector.Connector;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.logging.Logger;

/**
 * TODO: server内部组件，对应多个connector
 *
 * @author wuyijie
 * @date 2021/12/13 21:53
 */
public class Service extends BaseLiflecycleBean {
    private static final Logger logger = Logger.getLogger(Service.class.getName());


    private String name;
    private Engine engine;
    private List<Connector> connectorList;

    private ExecutorService executorService;

    public Service() {
    }

    public Service(String name, Engine engine, List<Connector> connectorList) {
        this.name = name;
        this.engine = engine;
        this.connectorList = connectorList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Engine getEngine() {
        return engine;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public List<Connector> getConnectorList() {
        return connectorList;
    }

    public void setConnectorList(List<Connector> connectorList) {
        this.connectorList = connectorList;
    }

    public ExecutorService getExecutorService() {
        return executorService;
    }

    public void setExecutorService(ExecutorService executorService) {
        this.executorService = executorService;
    }

    @Override
    public String toString() {
        return "Service{" +
                "name='" + name + '\'' +
                ", engine=" + engine +
                ", connectorList=" + connectorList +
                '}';
    }

    @Override
    public void init() {
        this.engine.init();
        for (Connector connector : this.connectorList) {
            connector.init();
        }
    }

    @Override
    public void start() {
        for (Connector connector : this.connectorList) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    logger.info("[Service starting : " + this + ", current thread : " + Thread.currentThread().getName() + "]");
                    connector.start();
                }
            });
            thread.start();


        }
    }
}
